package com.blb.trans.service.impl;

import com.blb.trans.service.StudentService;
import com.blb.trans.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TransactionServiceImpl implements TransactionService {

    @Autowired
    private StudentService studentService;

    @Override
    public void testTrans1(){
        //REQUIRED 外部方法没事务，内部方法有事务，内部方法事务新建独立的事务，分别提交
        //外部方法发生异常，内部方法事务不受影响
        studentService.updateRequired(1,12);
        studentService.updateRequired(2,12);
        throw new RuntimeException();
    }

    @Override
    public void testTrans2() {
        //独立的事务，内部没有异常，提交成功
        studentService.updateRequired(1,11);
        //独立的事务，内部有异常，回滚
        studentService.updateRequiredException(2,11);
    }

    @Transactional
    @Override
    public void testTrans3() {
        //外部方法和内部方法都有事务，内部方法是required，内部方法和外部方法事务会合并
        //外部方法出现异常，整体回滚
        studentService.updateRequired(1,13);
        studentService.updateRequired(2,13);
        throw new RuntimeException();
    }

    @Transactional
    @Override
    public void testTrans4() {
        //外部有事务，全部合并成一个，内部抛异常到外部，全部回滚
        studentService.updateRequired(1,14);
        studentService.updateRequiredException(2,14);
    }

    @Transactional
    @Override
    public void testTrans5() {
        studentService.updateRequired(1,15);
        //外部有事务，内部方法抛异常，进行处理后，全部回滚
        try {
            studentService.updateRequiredException(2, 15);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    @Transactional
    @Override
    public void testTrans6() {
        //子事务加入外部事务，一起回滚
        studentService.updateRequired(1,16);
        //子事务是独立，外部事务不能影响该事务
        studentService.updateRequiresNew(2,16);
        throw new RuntimeException();
    }

    @Transactional
    @Override
    public void testTrans7() {
        //外部事务收到异常，回滚
        studentService.updateRequired(1,11);
        //独立事务，成功提交
        studentService.updateRequiresNew(2,11);
        //独立事务，异常回滚
        studentService.updateRequiresNewException(3,11);
    }

    @Transactional
    @Override
    public void testTrans8() {
        //外部事务没有异常，正常提交
        studentService.updateRequired(1,13);
        //独立事务，正常提交
        studentService.updateRequiresNew(2,13);
        try {
            //独立事务，异常回滚，异常被处理
            studentService.updateRequiresNewException(3, 13);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    @Transactional
    @Override
    public void testTrans9() {
        //嵌套事务，外部事务异常，内部事务都回滚
        studentService.updateNested(1,18);
        studentService.updateNested(2,18);
        throw new RuntimeException();
    }

    @Transactional
    @Override
    public void testTrans10() {
        //嵌套事务，外部事务异常，内部事务都回滚
        studentService.updateNested(1,18);
        studentService.updateNestedException(2,18);
    }

    @Transactional
    @Override
    public void testTrans11() {
        //外部事务没异常，成功
        studentService.updateRequired(1,17);
        //嵌套事务，成功
        studentService.updateNested(2,17);
        try {
            //嵌套事务，异常回滚，异常被处理
            studentService.updateNestedException(3, 17);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    @Transactional
    @Override
    public void testTrans12() {
        //外部有事务就加入，异常回滚
        studentService.updateSupports(1,19);
        studentService.updateSupports(2,19);
        throw new RuntimeException();
    }

    @Override
    public void testTrans13() {
        //外部没有事务，就非事务执行，全部更新后再抛异常，不会回滚
        studentService.updateSupports(1,19);
        studentService.updateSupportsException(2,19);
    }

    @Transactional
    @Override
    public void testTrans14() {
        //外部事务异常，回滚
        studentService.updateRequired(1,27);
        //没有事务，提交成功
        studentService.updateNotSupports(2,27);
        //没有事务，提交成功
        studentService.updateNotSupportsException(3,27);
    }

//    @Transactional
    @Override
    public void testTrans15() {
        //只要有事务，抛异常
        studentService.updateNever(1,18);
    }

//    @Transactional
    @Override
    public void testTrans16() {
        //外部有事务，就加入，外部没事务，就抛异常
        studentService.updateMandatory(1,88);
    }
}
